﻿using System;
using System.Collections.Generic;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Input;

namespace Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls
{
    public class Textbox : Label
    {
        public int CharacterLimit { get; set; }

        private static readonly List<string> AllowedCharacters = new List<string>
            {
                "01-\b",
                "0123456789-\b",
                "0123456789ABCDEFabcdef-\b",
                "-0123456789.\b",
                "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\b ",
                "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-0123456789.\b "
            };


        public enum TextboxFilter
        {
            Binary = 0,
            Integer,
            Hexadecimal,
            Float,
            Alpha,
            Alphanumeric,
            None,
        }

        public TextboxFilter Filter { get; set; }



        public Textbox(Func<string, Sprite> getSpriteMethod, string defaultText = "", int characterLimit = -1) : base(getSpriteMethod, defaultText)
        {
            CharacterLimit = characterLimit;
            KeyboardHandler.OnWriteText += OnTextEntered;
            Filter = TextboxFilter.None;
        }

        private void OnTextEntered(string input)
        {
            //Filter unallowed characters
            if (Filter != TextboxFilter.None && !AllowedCharacters[(int) Filter].Contains(input))
                return;

            if (input == "\b")
            {
                if (Text[0].Length > 0)
                    Text[0] = Text[0].Substring(0, Text[0].Length - 1);
            }
            else if (Text[0].Length != CharacterLimit)
                Text[0] += input;
        }

    }
}